package alarm;

public class User implements Runnable {
    private String name;
    private Alarm alarm;

    public User(String name, Alarm alarm) {
        this.name = name;
        this.alarm = alarm;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Alarm getAlarm() {
        return alarm;
    }

    public void setAlarm(Alarm alarm) {
        this.alarm = alarm;
    }

    public void stopAlarm() {
        synchronized (alarm) {
            if (alarm.isWork()) {
                alarm.setWork(false);
                System.out.println(this.name + "按了闹钟！");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    alarm.notify();
                    try {
                        alarm.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                try {
                    alarm.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void run() {
        while (true) {
            stopAlarm();
        }
    }
}
